<template>
	<view class="parent">
		<text class="title-tip">认证信息仅用于实名认证，会严格保密</text>
		<view class="content">
			<text class="label">真实姓名</text>
			<input :placeholder="nameHint" v-model="name"/>
		</view>
		<view class="content">
			<text class="label">身份证号</text>
			<input :placeholder="idHint" v-model="id"/>
		</view>
		<!-- <view class="image">
			<view class="label" @click='choosePhoto(1)'>
				<text class="label">身份证正面</text>
				<image :src="getImageUrl('ic_photo.png')" ></image>
			</view>
			<image :src="idCardFront.url"></image>
		</view>
		<view class="image">
			<view class="label" @click='choosePhoto(2)'>
				<text class="label">身份证反面</text>
				<image :src="getImageUrl('ic_photo.png')"></image>
			</view>
			<image :src="idCardBack.url"></image>
		</view> -->
		<view class="btn" @click="onSubmit">立即认证</view>
		<view class="tip">
			一个身份信息只能绑定一个账号，证件信息绑定后不能修改。
		</view>
	</view>
</template>

<script>
	import {
		ossUpload,
		getFileName
	} from '@/js_sdk/jason-alioss-upload/oss.js'
	
	export default {
		data() {
			return {
				name: '',
				id: '',
				nameHint: '请输入姓名',
				idHint: '请输入身份证号',
				idCardFront:{
					url: '', 
					id: ''
				},
				idCardBack: {
					url: '',
					id: ''
				},
			}
		},
		methods: {
			choosePhoto(type) {
				console.log('choosePhoto');
				const carmeraAvatar = uni.getStorageSync('carmeraRealname');
				if (carmeraAvatar) {
					this.uploadOOS(type)
					return
				}
				uni.showModal({
					content: '拍照功能用于实名认证',
					success: res => {
						if (res.confirm) {
							uni.setStorageSync('carmeraRealname', true);
							this.uploadOOS(type)
						}
					}
				})
			},
			uploadOOS(type) {
				uni.chooseImage({
					count: 1,
					success: async (file) => {
						let dir = 'userinfo'
						let fileName = getFileName(file.tempFilePaths[0]);
						const {
							success,
							data
						} = await ossUpload(file.tempFilePaths[0], fileName, dir)
						console.log("data: " + JSON.stringify(data));
						if (success) {
							this.requestFile(dir, fileName, data, type)
						} else {
							uni.showToast({
								icon: 'none',
								title: data
							})
						}
					}
				});
			},
			async requestFile(pathKey, fileNameKey, picUrl, type) {
				let params = {
					pathKey: pathKey,
					userIdKey: uni.getStorageSync('userId'),
					fileNameKey: fileNameKey
				}
				let res = await this.$common.post('/api/ossSts/callBackFiInfo', params)
				if (200 == res.code) {
					if(1 == type){
						this.idCardFront = {
							url: picUrl,
							id: res.data
						}
					}else{
						this.idCardBack = {
							url: picUrl,
							id: res.data
						}
					}
				}
			},
			onSubmit(){
				if('' == this.name){
					uni.showToast({
						title: this.nameHint,
					})
					return
				}
				if('' == this.id){
					uni.showToast({
						title: this.idHint,
					})
					return
				}
				// if('' == this.idCardFront.id){
				// 	uni.showToast({
				// 		title: '请选择身份证正面',
				// 	})
				// 	return
				// }
				// if('' == this.idCardBack.id){
				// 	uni.showToast({
				// 		title: '请选择身份证反面',
				// 	})
				// 	return
				// }
				this.requestSave();
			},
			
			async requestSave(){
				let params = {realName: this.name, idNum: this.id};
				let res = await this.$common.post("/app/auth/realNameAuthIos", params);
				if(0 == res.code){
					uni.showToast({
						title: '提交成功',
					});
					uni.navigateBack();
				}else{
					uni.showToast({
						icon:'none',
						title: res.msg,
					});
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.parent{
		display: flex;
		flex-direction: column;
		padding: 50rpx;
		justify-content: center;
		
		.title-tip{
			color: #222;
			font-size: 24rpx;
			text-align: center;
		}
		
		.content{
			display: flex;
			flex-direction: row;
			padding: 0 10rpx;
			align-items: center;
			width: 100%;
			height: 80rpx;
			background-color: #eee;
			border-radius: 20rpx;
			margin-top: 50rpx;
			.label{
				color: #222;
				font-size: 30rpx;
				width: 150rpx;
				text-align: center;
			}
			input{
				color: #666;
				font-size: 28rpx;
			}
		}
		
		.image{
			display: flex;
			flex-direction: column;
			padding: 0 20rpx;
			margin-top: 20rpx;
			width: 100%;
			.label{
				display: flex;
				flex-direction: row;
				align-items: center;
				color: #222;
				font-size: 30rpx;
				image{
					width: 50rpx;
					height: 50rpx;
					margin-left: 20rpx;
				}
			}
			image{
				width: 100%;
				height: 350rpx;
				border-radius: 5rpx;
			}
		}
		
		.tip{
			color: #999;
			font-size: 20rpx;
			margin-top: 20rpx;
		}
	}
	
	.btn{
		margin: 100rpx 0 0;
		box-sizing: border-box;
		background-image: linear-gradient(to right, #9fe9ff, #ab7aff);
		border-radius: 40rpx;
		width: 100wh;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		color:white;
	}
</style>
